package com.kjlink.internal.report4vue.project.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.xuqiudong.common.base.model.BaseResponse;
import cn.xuqiudong.common.base.model.PageInfo;
import cn.xuqiudong.common.base.vo.Select2VO;
import com.kjlink.internal.report4vue.base.controller.BaseApiController;
import com.kjlink.internal.report4vue.project.request.AddProjectUserRequest;
import com.kjlink.internal.workreport.base.enums.ProjectStatus;
import com.kjlink.internal.workreport.project.model.Project;
import com.kjlink.internal.workreport.project.service.ProjectService;
import com.kjlink.internal.workreport.report.service.WorkReportService;
import com.kjlink.internal.workreport.user.lookup.UserListLookup;
import com.kjlink.internal.workreport.user.model.User;
import com.kjlink.internal.workreport.user.service.UserService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import java.util.List;

/**
 * 描述:
 *
 * @author Vic.xu
 * @since 2024-12-12 9:21
 */
@RestController
public class ApiProjectController extends BaseApiController {

    @Resource
    private ProjectService projectService;

    @Resource
    private UserService userService;

    @Resource
    private WorkReportService workReportService;

    /**
     * 列表页
     */
    @RequestMapping(value = "/project/list", method = {RequestMethod.GET, RequestMethod.POST})
    public BaseResponse<PageInfo<Project>> index(@RequestBody Project lookup) {
        PageInfo<Project> pager = projectService.page(lookup);
        return BaseResponse.success(pager);
    }


    /**
     * 详细
     */
    @GetMapping("/project/detail/{id}")
    public BaseResponse<Project> edit(@PathVariable Integer id) {
        Project entity = projectService.findById(id);
        return BaseResponse.success(entity);
    }

    /**
     * name是否重复
     */
    @RequestMapping("/project/checkName/{name}/{id}")
    public BaseResponse<Boolean> checkCode(@PathVariable Integer id, @PathVariable String name) {
        boolean success = projectService.checkNotRepeat(id, name, "name");
        return BaseResponse.success(success);
    }

    /**
     * save project
     */
    @PostMapping("/project/save")
    public BaseResponse<Integer> save(@RequestBody Project entity) {
        int num = projectService.save(entity);
        return BaseResponse.success(num);
    }

    /**
     * 项目下的用户列表
     *
     * @param id 项目id
     */
    @GetMapping("/project/projectUser/{id}")
    public BaseResponse<List<User>> projectUser(@PathVariable("id") Integer id) {
        List<User> usersByProjectId = userService.findUsersByProjectId(id);
        return BaseResponse.success(usersByProjectId);
    }

    /**
     * 供项目选择的人员列表:  尚未被选择的其他人员
     */
    @PostMapping("/project/selectUser")
    public BaseResponse<List<User>> selectProjectUser(@RequestBody UserListLookup lookup) {
        Integer projectId = lookup.getProjectId();
        if (projectId == null) {
            return BaseResponse.error("项目id不能为空");
        }
        List<User> list = userService.userList(lookup);
        return BaseResponse.success(list);
    }

    /**
     * 新增项目人员
     */
    @PostMapping("/project/addProjectUser")
    public BaseResponse<Integer> addProjectUser(@RequestBody AddProjectUserRequest projectUserRequest) {
        int num = projectService.addProjectUser(projectUserRequest.getProjectId(), projectUserRequest.getUserIds());
        return BaseResponse.success(num);
    }

    /**
     * 删除项目人员
     */
    @PostMapping("/project/delete/{projectId}/{userId}")
    public BaseResponse<Integer> deleteProjectUser(@PathVariable Integer userId, @PathVariable Integer projectId) {
        int num = projectService.deleteProjectUser(userId, projectId);
        return BaseResponse.success(num);
    }

    /**
     * 我的可选项目
     */
    @RequestMapping(value = "/project/select/mine")
    public BaseResponse<List<Select2VO>> mine() {
        int userId = StpUtil.getLoginIdAsInt();
        List<Select2VO> list = projectService.mineProject(userId);
        return BaseResponse.success(list);
    }

    /**
     * 项目下拉列表
     */
    @RequestMapping(value = "/project/select/all")
    public BaseResponse<List<Select2VO>> all() {
        List<Select2VO> list = projectService.mineProject(null);
        return BaseResponse.success(list);
    }

    /**
     * 我的可用项目下拉列表 新增日报的时候使用
     */
    @RequestMapping(value = "/project/select/mineUsable")
    public BaseResponse<List<Select2VO>> mineUsableProject() {
        int userId = StpUtil.getLoginIdAsInt();
        List<Select2VO> list = projectService.mineUsableProject(userId);
        return BaseResponse.success(list);
    }


    /**
     * 删除
     */
    @PostMapping("/project/delete/{id}")
    public BaseResponse delete(@PathVariable Integer id, RedirectAttributes attrs) {
        boolean exists = workReportService.existsWorkReport(id);
        if (exists) {
            return BaseResponse.error("当前项目已存在日报，无法删除");
        }
        int num = projectService.delete(id);
        return BaseResponse.success(num);
    }

    /**
     * 修改状态
     */
    @PostMapping("/project/updateStatus/{status}/{id}")
    public BaseResponse<Integer> updateStatus(@PathVariable Integer id, @PathVariable ProjectStatus status) {
        Project project = new Project();
        project.setId(id);
        project.setStatus(status.getCode());
        int num = projectService.update(project);
        return BaseResponse.success(num);
    }

}
